package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.commom.exception.CustException;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.model.behavior.dtos.LikesBehaviorDTO;
import com.heima.model.behavior.pojo.ApBehaviorEntry;
import com.heima.model.behavior.pojo.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.NewBehaviorDTO;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojo.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author by jojo
 * @Date 2022/3/8
 * @Description
 */
@Service
@Slf4j
public class ApLikesBehaviorServiceImpl implements ApLikesBehaviorService {
    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDTO dto) {
        //1.参数校验
        //是否登录
        ApUser apUser = AppThreadLocalUtils.getUser();
        if (apUser==null) {
            log.error("需要登录后才可以点赞");
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        Short operation = dto.getOperation();
        Long articleId = dto.getArticleId();
        //2.根据当前登录用户id查询行为实体对象
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
        String entryId = apBehaviorEntry.getId();
        // 3. 如果是点赞操作
        if (operation==(short)0){
            //判断是否已经点过赞
            Query query = Query.query(Criteria.where("articleId").is(articleId).and("entryId").is(entryId));
            ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (one!=null) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_ALLOW,"已经点赞了，不能重复点赞哦！！！");
            }
            // 4. 没有点过赞则 像mongo点赞集合中 添加点赞数据
            //封装数据
            ApLikesBehavior apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setArticleId(articleId);
            apLikesBehavior.setType((short)0);
            apLikesBehavior.setEntryId(apBehaviorEntry.getId());
            apLikesBehavior.setCreatedTime(new Date());
            mongoTemplate.save(apLikesBehavior);
        }else{
            // 5. 如果是取消点赞操作  在mongo点赞集合中 删除对应点赞数据
            Query query = Query.query(Criteria.where("articleId").is(articleId).and("entryId").is(entryId));
            mongoTemplate.remove(query,ApLikesBehavior.class);
        }
        //*****添加文章点赞-发送消息******
        NewBehaviorDTO messDto = new NewBehaviorDTO();
        messDto.setType(NewBehaviorDTO.BehaviorType.LIKES);
        messDto.setArticleId(dto.getArticleId());
        messDto.setAdd(dto.getOperation()==(short)0?1:-1);
        rabbitTemplate.convertAndSend(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE, JSON.toJSONString(messDto));
        log.info("点赞行为发送MQ消息成功  ==》参数={}",messDto);
        return ResponseResult.okResult();
    }
}
